<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\dao\energy;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\energy\EnergyQrcodeModel;

class EnergyQrcodeDao extends BaseDao
{

    /**
     * @return EnergyQrcodeModel
     */
    protected function getModel(): string
    {
        return EnergyQrcodeModel::class;
    }

    /**
     * @param array $where
     * @return BaseQuery
     */
    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['energy_type']) && $where['energy_type'] !== '', function ($query) use ($where) {
                $query->where('energy_type', intval($where['energy_type']));
            })
            ->when(isset($where['energy_id']) && $where['energy_id'] !== '', function ($query) use ($where) {
                $query->where('energy_id', intval($where['energy_id']));
            })
            ->when(isset($where['is_bind']) && $where['is_bind'] !== '', function ($query) use ($where) {
                $query->where('is_bind', intval($where['is_bind']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('energy_code|energy_no|remark', '%' . trim($where['keywords']) . '%');
            })
            ->when(isset($where['add_time']) && $where['add_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['add_time'], 'add_time');
            });
    }


}
